﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.ServiceModel;
using BookClubIt.Processor;
using DTO = BookClubIt.DTO;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

namespace BookClubIt
{


    public partial class message : BasePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
          
        }

        private void BindData()
        {

            DTO.BookClub club = BookClubProcessorInstance.GetBookClubById(CID);

            DTO.Members get_members = MemberProcessorInstance.GetBookClubMembersById(CID);

            lbMembers.DataSource = get_members;
            lbMembers.DataTextField = "Username";
            lbMembers.DataValueField = "Id";

            lbMembers.DataBind();
        }
        protected void lnbSendMessage_Click(Object sender, EventArgs e)
        {

            for (int i = 0; i < lbMembers.Items.Count; i++)
            {
                if (lbMembers.Items[i].Selected)
                {
                    int selectedItem = Convert.ToInt32(lbMembers.Items[i].Value);
                    BookClubProcessorInstance.SendBookClubMessage(CID, CurrentUserId, selectedItem, txtMessage.InnerText.ToString());

                    DTO.Member member = MemberProcessorInstance.GetMemberById(Convert.ToInt32(lbMembers.Items[i].Value));

                    MailMessage mail = new MailMessage();

                    mail.Subject = "You have a message!";

                    mail.Body = string.Format(@"<html>
                                        <head>
                                        </head>
                                        <body>
                                        <table width='600px' cellpadding='0' cellspacing='0' border='1px' bordercolor='#0088CC'>
                                        <tr><td bgcolor='#0088CC' style='padding:5px;'><img src='http://www.bookclubit.com/images/logo.png'/></td></tr>
                                        <tr><td style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>Hello,</p>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>{0} sent you a message at <a href='http://www.bookclubit.com'>bookclubit.com</a> that you can view below:</p>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;padding:5px;backround:#eeeeee'>{1}</p>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>To view the message, simply <a href='http://www.bookclubit.com/account/inbox.aspx'>click here</a> and see who contacted you.</p>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>Thanks,</p>
                                        <p style='font:Georgia, 'Times New Roman', Times, serif;font-size:12px;'>Bookclubit.com</p>
                                        </td></tr>
                                        </table>
                                        </body>
                                        </html>", member.Username, txtMessage.InnerText);
                    mail.IsBodyHtml = true;
                    mail.From = new MailAddress("noreply@bookclubit.com", "bookclubit");
                    mail.To.Add(new MailAddress(member.Email.ToString()));

                    //create instance of smtpclient
                    SmtpClient smtp = new SmtpClient();
                    smtp.Port = 2525;
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = new NetworkCredential("noreply@bookclubit.com", "password1");
                    smtp.Host = "mail.bookclubit.com";
                    smtp.EnableSsl = false;
                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                    try
                    {            
                        smtp.Send(mail);
                        pnlMessage.Visible = true;
                        lblMsg.Text = "Your message has been successfully sent.";
                    }
                    catch (SmtpFailedRecipientException ex)
                    {
                        pnlMessage.Visible = true;
                        lblMsg.Text = "Your message was not sent - " + ex.GetBaseException();
                    }

                }
            }
            BindData();
        }
        
    }
}